home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
simula
/
books
/
books.lha
/
kirkerud
/
studset2.sim
< prev
next >
Wrap
Text File
|
1993-08-16
|
13KB
|
336 lines
% ****************************************************************
% * *
% * This is a revised version the program constructed in *
% * example 14.1 of *
% * Object Oriented Programming with Simula by Bj|rn Kirkerud; *
% * The revisions in the program consist in the use of *
% * a few classes containing generally useful tools *
% * *
% ****************************************************************
external class Settools;
Settools begin
% ****************************************************************
% * *
% * Declarations of classes containing generally usefull tools: *
% * *
% ****************************************************************
external class promptools, texttools, filetools;
ref(promptools) pt;
ref(texttools) tt;
ref(filetools) ft;
procedure outline(line); text line;
begin outtext(line); outimage end;
% ****************************************************************
% * *
% * The class Student: *
% * *
% ****************************************************************
element class Student(ident); text ident;
begin
! Variables to keep the data that describe this student: ;
integer year, month, day, form;
Boolean female;
character math_grade, eng_grade, hist_grade;
! A procedure with a value which is unique for this student: ;
text procedure key; key :- ident;
! A variable to hold a reference to the next Student in a pointer chain: ;
ref(Student) next_in_chain;
! Data access procedures: ;
procedure read;
begin
year := pt.ask_for_int("Year of birth? ");
month := pt.ask_for_int("Month? ");
day := pt.ask_for_int("Day? ");
form := pt.ask_for_int("Form? ");
female := pt.ask_for_bool("Female? ");
math_grade := pt.ask_for_char("Grade in mathematics? ");
eng_grade := pt.ask_for_char("Grade in English? ");
hist_grade := pt.ask_for_char("Grade in history? ");
end of Student'read;
procedure display;
begin
outtext("Data for student " & ident);
outtext(". Born: ");
outint(day, 2); outchar('/');
outint(month, 2); outchar('/');
outint(year, 4);
outline(". " & (if female then "Female" else "Male") & ". ");
outline("Form: " & tt.int_as_text(form) & ". " &
"Current grades: " &
"Mathematics: " & tt.char_as_text(math_grade) & ", " &
"English: " & tt.char_as_text(eng_grade) & ", " &
"History: " & tt.char_as_text(hist_grade) & ".");
end of Student'display;
procedure change;
begin character attribute;
attribute := pt.ask_for_char("What do you want to change? ");
if attribute = 'y' then
year := pt.ask_for_int("New birth year? ") else
if attribute = 'm' then
month := pt.ask_for_int("New birth month? ") else
if attribute = 'd' then
day := pt.ask_for_int("New day of birth? ") else
if attribute = 'f' then
form := pt.ask_for_int("New form number? ") else
if attribute = 's' then
female := pt.ask_for_bool("Female? ") else
if attribute = 'a' then
math_grade := pt.ask_for_char("New grade in math? ") else
if attribute = 'e' then
eng_grade := pt.ask_for_char("New grade in English? ") else
if attribute = 'h' then
hist_grade := pt.ask_for_char("New grade in history? ")
else begin
outline("You can change one of the following attributes:");
outline(" m: Birth month");
outline(" d: Day of birth");
outline(" f: Form number");
outline(" s: Sex");
outline(" a: Grade in mathematics");
outline(" e: Grade in English");
outline(" h: Grade in history");
change;
end;
end of Student'change;
character procedure worst_grade;
worst_grade := max(math_grade, max(eng_grade, hist_grade));
procedure put_in_record(outf); ref(outfile) outf;
begin
outf.outtext(ident); outf.setpos(11);
ft.int_to_file(outf, year);
ft.int_to_file(outf, month);
ft.int_to_file(outf, day);
ft.int_to_file(outf, form);
ft.bool_to_file(outf, female);
outf.outchar(math_grade);
outf.outchar(eng_grade);
outf.outchar(hist_grade);
end;
procedure get_from_record(inf); ref(infile) inf;
begin
year := ft.int_from_file(inf);
month := ft.int_from_file(inf);
day := ft.int_from_file(inf);
form := ft.int_from_file(inf);
female := ft.bool_from_file(inf);
math_grade := inf.inchar;
eng_grade := inf.inchar;
hist_grade := inf.inchar;
end;
end of Student;
% ****************************************************************
% * *
% * Declaration of a variable to keep a reference to *
% * an ordered set of Students: *
% * *
% ****************************************************************
ref(ordered_set) The_students;
% ****************************************************************
% * *
% * Declarations of command procedures: *
% * *
% ****************************************************************
procedure Give_help;
begin
outline("The legal commands are: ");
outline(" ?: Help (writes this text)");
outline(" N: To enter data about a new student");
outline(" W: Writes data about a specified student");
outline(" L: Writes a list with all students");
outline(" C: Changes data about a specified student");
outline(" R: Removes all data about a specified student");
outline(" P: Puts all data to file ""stud.dta""");
outline(" G: Gets data from file ""stud.dta""");
outline(" B: Writes students with bad grades");
outline(" Q: Quit (the program execution stops)");
end of Give_help;
procedure Enter_student;
begin ref(Student) a_student; Boolean ident_exists;
a_student :- new Student(pt.ask_for_text("Identity number? "));
The_students.add_element(a_student, ident_exists);
if ident_exists
then outline("The identity number " & a_student.key &
" is already in use!")
else begin
a_student.read;
outline("The data for student " & a_student.key &
" have been stored.");
end;
end of Enter_student;
procedure Write_student;
begin ref(Student) a_student;
a_student :- The_students.find_element(pt.ask_for_text("Id. number? "));
if a_student == none
then outline("No student with that identity number!")
else a_student.display;
end of Write_student;
procedure List_students;
if The_students.is_empty
then outline("Data for no students have been entered")
else begin ref(Student) a_student;
outline("Data for " & tt.int_as_text(The_students.size) &
" students have been entered. " &
" These data are as follows:");
a_student :- The_students.first_element;
while a_student =/= none do
begin
a_student.display;
a_student :- The_students.next_element;
end;
end of List_students;
procedure Change_student;
begin ref(Student) a_student;
a_student :- The_students.find_element(pt.ask_for_text("Id. number? "));
if a_student == none
then outline("No student with that identity number!")
else begin a_student.display; a_student.change end;
end of Change_student;
procedure Remove_student;
begin integer ident_number; Boolean no_such_student;
ident_number := pt.ask_for_int("Identity number? ");
The_students.Remove_element(pt.ask_for_text("Id. number? "),
no_such_student);
if no_such_student
then outline("No student with that identity number!")
else outline("The student has been removed!");
end of Remove_student;
procedure Put_to_file;
inspect new outfile(pt.ask_for_text("Write name of outfile> ")) do
begin ref(Student) a_student;
open(blanks(28));
a_student :- The_students.first_element;
while a_student =/= none do
begin
a_student.put_in_record(this outfile);
outimage;
a_student :- The_students.next_element;
end;
close;
end;
procedure Get_from_file;
inspect new infile(pt.ask_for_text("Write name of infile> ")) do
if open(blanks(28))
then begin ref(Student) a_student;
inimage;
while not endfile do
begin
a_student :- new Student(intext(10).strip);
a_student.get_from_record(this infile);
The_students.add_element_ok(a_student);
inimage;
end;
close;
end
else outline("File " & filename & " can not be opened for reading");
procedure Bad_grades;
begin character grade_limit; ref(Student) a_student;
grade_limit := pt.ask_for_char("Grade limit? ");
a_student :- The_students.first_element;
while a_student =/= none do
begin
if a_student.worst_grade ge grade_limit then a_student.display;
a_student :- The_students.next_element;
end;
end of Bad_grades;
procedure Unknown_command(c); character c;
begin
outline(" You gave the command '" & tt.char_as_text(c) & "'" &
" This is not among the legal commands.");
outline(" Type ? if you don't remember the legal commands");
end of Unknown_command;
% ****************************************************************
% * *
% * Declaration of a variable to keep the latest command *
% * typed by the user: *
% * *
% ****************************************************************
character command;
% ****************************************************************
% * St*
% * That was the last declaration. *
% * Now come the imperatives of the program: *
% * S *
% ****************************************************************
% * First, initialization of the data-structure: ;
The_students :- new ordered_set;
% * then, initialization of tool-boxes: ;
pt :- new promptools;
tt :- new texttools;
ft :- new filetools;
% * Finally, read and execute commands typed by the user: ;
command := pt.ask_for_char("Type your first command (? for help) > ");
while command ne 'Q' do
begin
if command = '?' then Give_help else
if command = 'N' then Enter_student else
if command = 'W' then Write_student else
if command = 'L' then List_students else
if command = 'C' then Change_student else
if command = 'R' then Remove_student else
if command = 'P' then Put_to_file else
if command = 'G' then Get_from_file else
if command = 'B' then Bad_grades
else Unknown_command(command);
command := pt.ask_for_char("Your next command > ");
end;
outline("Bye");
end